/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.joseph.violation.ocp;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Brad'ey
 */
public class SoundManager{    
    private List<Animal> animalList;            
    public SoundManager()
    {        
        animalList = new ArrayList<Animal>();    
    }    
    
    public void add(Animal a)
    {        
        animalList.add(a);    
    }
    
    /*
     * "This is not a good solution because suppose a new class Lion 
     * comes into picture, and it has a sound called roar, 
     * you have to edit the code and another if else or switch() 
     * to make that work. So it is violation of OCP."
     */
    
    public void makeSound()
    {        
        for(Animal a : animalList) {            
            if(a instanceof Cat) {
                makeMew(a);            
            }            
            else if(a instanceof Dog) {
                makeBark(a);
            }
        }   
    }    
    
    public void makeMew(Animal a)
    {        
        System.out.println(a.getSound());   
    }    
    
    public void makeBark(Animal a)
    {        
        System.out.println(a.getSound());   
    
    }
    /*
     * It should be clear that no significant program can be 100% closed. 
     * So we may need to change the code depending on the requirement, 
     * but we can try to make our code as robust as possible. 
     */
}
